package com.example.chatuidemo.slice;
import com.example.chatuidemo.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;
import java.util.ArrayList;

public class ContactPageListProvider extends BaseItemProvider {
    private ArrayList<ContactModel> datalist;
    private Context context;
    public ContactPageListProvider(Context context) {
        super();
        this.context = context;
        setupData();
    }
    private void setupData() {
        datalist = new ArrayList<ContactModel>();
        for (int i = 0; i < 20; i++) {
            ContactModel model = new ContactModel(ResourceTable.Media_icon, String.format("联系人%d", i));
            datalist.add(model);
        }
    }
    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int i) {
        return datalist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_contact_item, null, false);
        } else {
            cpt = convertComponent;
        }
        Image image = (Image) cpt.findComponentById(ResourceTable.Id_image);
        Text name = (Text) cpt.findComponentById(ResourceTable.Id_name_text);

        ContactModel model = datalist.get(i);
        image.setPixelMap(model.icon);
        name.setText(model.name);
        return cpt;
    }
}
